<template>
    <!-- 对话框表单 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="baTable.form.operate ? true : false"
        @close="baTable.toggleForm"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    label-position="right"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                    class="flex"
                    :disabled="baTable.form.operate == 'view'"
                >
<!--                    <FormItem :label="t('aREA.CORP')" type="string" v-model="baTable.form.items!.CORP" prop="CORP" :input-attr="{ placeholder: t('Please input field', { field: t('aREA.CORP') }) }" />-->
                    <FormItem
                        :label="t('aREA.CORP')"
                        v-model="baTable.form.items!.CORP"
                        type="remoteSelect"
                        :input-attr="{
                            pk: 'corp',
                            field: 'corp',
                            'remote-url': codelib  + 'corps',
                            disabled:type?true:isYard?true:false
                        }"
                        placeholder="点击选择公司"
                    />
                    <FormItem
                    v-if="!isYard"
                    :label="t('aREA.areatype')"
                    prop="areatype"
                    v-model="baTable.form.items!.areatype"
                    type="radio"
                    :data="{ content: { '1': t('客户工厂'), '2': t('堆场') }, childrenAttr: { border: true } }"
                    :input-attr="{
                        onchange:handleAreaChange,  
                        disabled:isYard                     
                    }"
                    />
                    <FormItem
                    v-if="isYard"
                    :label="t('aREA.areatype')"
                    prop="areatype"
                    v-model="baTable.form.items!.areatype"
                    type="radio"
                    :data="{ content: { '2': t('堆场') }, childrenAttr: { border: true } }"
                    :input-attr="{
                        onchange:handleAreaChange                        
                    }"
                    />

                    <FormItem :label="t('aREA.REMIND')" type="string" v-model="baTable.form.items!.REMIND" prop="REMIND" :input-attr="{ placeholder: t('Please input field', { field: t('aREA.REMIND') }) ,disabled:baTable.form.operate!=='add'&&!isYard }" />
                    <FormItem
                        :label="t('aREA.PORT')"
                        prop="PORT"
                        v-show="baTable.form.items!.areatype!=='2'"
                        v-model="baTable.form.items!.PORT"
                        type="radio"
                        :data="{ content: { '装货地': t('装货地'), '送货地': t('送货地') }, childrenAttr: { border: true } }"
                        :input-attr="{
                            onchange:handleAreaChange,

                        }"
                    />
                    <FormItem
                        :label="t('aREA.PORT')"
                        v-show="baTable.form.items!.areatype=='2'"
                        prop="PORT"
                        v-model="baTable.form.items!.PORT"
                        type="radio"
                        :data="{ content: { '堆场': t('堆场') }, childrenAttr: { border: true } }"
                        :input-attr="{
                            onchange:handleAreaChange,
                            disabled:isYard
                        }"
                    />
                    <FormItem :label="t('aREA.linkman')" type="string" v-model="baTable.form.items!.linkman" prop="linkman" :input-attr="{ placeholder: t('Please input field', { field: t('aREA.linkman') }) }" />
                    <FormItem :label="t('aREA.AREA')" type="string" v-model="baTable.form.items!.AREA" prop="AREA" :input-attr="{ placeholder: t('Please input field', { field: t('aREA.AREA') }) }" />
                    
                    <FormItem :label="t('aREA.tel')" type="string" v-model="baTable.form.items!.tel" prop="tel" :input-attr="{ placeholder: t('Please input field', { field: t('aREA.tel') }) }" />
                    
                    <FormItem :label="t('aREA.fax')" type="string" v-model="baTable.form.items!.fax" prop="fax" :input-attr="{ placeholder: t('Please input field', { field: t('aREA.fax') }) }" />
                    <template v-if="isYard">
                        <FormItem :label="t('aREA.DEMO')" type="textarea" v-model="baTable.form.items!.DEMO" prop="DEMO" :input-attr="{ placeholder: t('Please input field', { field: t('aREA.DEMO') }) }" />
                        <FormItem :label="t('aREA.REMARK')" type="textarea" v-model="baTable.form.items!.REMARK" prop="REMARK" :input-attr="{ placeholder: t('Please input field', { field: t('aREA.REMARK') }) }" />
                    </template>
                    <template v-else>
                        <FormItem style="width:100%" :label="t('aREA.AREANAME')" type="textarea" v-model="baTable.form.items!.AREANAME" prop="AREANAME" :input-attr="{ placeholder: t('Please input field', { field: t('aREA.AREANAME') }) }" />
                    </template>

                    <FormItem
                        :label="t('aREA.OP')"
                        type="string"
                        v-model="baTable.form.items!.OP"
                        prop="OP"
                        :input-attr="{ placeholder: '当前登录用户','disabled':true }"
                    />
                    <FormItem :label="t('aREA.OPTIME')"
                              type="date"
                              v-model="baTable.form.items!.OPTIME"
                              prop="OPTIME"
                              :input-attr="{ placeholder: '当前日期','disabled':true  }" />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer v-if="baTable.form.operate !== 'view'">
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm('')">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="handleSave" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref, inject ,watchEffect, computed} from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import FormItem from '/@/components/formItem/index.vue'
import type { ElForm, FormItemRule } from 'element-plus'
import { buildValidatorData } from '/@/utils/validate'
import { codelib } from '/@/api/controllerUrls'
import { useRoute } from 'vue-router'


const formRef = ref<InstanceType<typeof ElForm>>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    
    areatype: [buildValidatorData({name: 'required', message: t('Please enter the correct field', { field: t('aREA.areatype') })})],
    PORT: [buildValidatorData({name: 'required', message: t('Please enter the correct field', { field: t('aREA.PORT') })})],
    OPTIME: [buildValidatorData({name: 'date', message: t('Please enter the correct field', { field: t('aREA.OPTIME') })})],
})


const route = useRoute()
const type = route.path === '/admin/corplib/customer'? 1:route.path === '/admin/corplib/supplier'||route.path === '/admin/corplib/supplier/approve'?2:null

// 是否是供应商管理里的堆场门点管理
const isYard = computed(()=>{
    return route.name === 'aREAYard'
})


const companyName = inject('companyName') as String
watchEffect(()=>{
    if(baTable.form.operate == 'add'){
        baTable.form.items!.CORP = companyName.value || ''
        baTable.form.items!.areatype = '1'
        if(type == 2){
            baTable.form.items!.areatype = '2'
            baTable.form.items!.PORT = '堆场'
        }
    }
})

const handleSave= ()=>{
    baTable.form.items!.type = type
    baTable.onSubmit(formRef.value)
}

const handleAreaChange = (val:string)=>{
    console.log(val)
}


</script>

<style scoped lang="scss"></style>
